package com.pw.demo3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pw.demo3.annotation.PerAnnotion;
import com.pw.demo3.bean.Permission;
import com.pw.demo3.service.impl.PermissionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/permission")
public class permissionController {

    @Autowired
    PermissionServiceImpl permissionService;

    @RequestMapping("/list")
    @PerAnnotion(name = "权限列表",expression = "permission:list")
    public String permissionList(Model model,
                                 String currentPage) {
        int pageNum = 1;
        if (currentPage != null) {
            pageNum = Integer.parseInt(currentPage);
        }
        PageHelper.startPage(pageNum, 5);
        List<Permission> permissionList = permissionService.selectAll();
        PageInfo<Permission> pageInfo = new PageInfo<>(permissionList);
        model.addAttribute("pageInfo", pageInfo);
        return "/views/permission/list";
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    @PerAnnotion(name = "权限删除",expression = "permission:delete")
    public Map<String, Object> permissionDelete(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            permissionService.deleteByPrimaryKey(Long.valueOf(id));
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "错误");
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/reSet")
    public String reSet(){
        permissionService.reSet();
        return "redirect:/permission/list";
    }
}
